<template>
  <div class="home">
    <top class="wow fadeInDown" title="智慧运维视频监控智能分析系统"></top>
    <div class="homeMain">
      <item>
        <div class="pageInner">
          <breadcrumb>
            <el-breadcrumb-item>告警查询</el-breadcrumb-item>
          </breadcrumb>
          <div class="searchTop">
            <div class="searchl">
              <!-- 标题和日期选择 -->
              <div class="searchls">
                <span class="label2">时间：</span>
                <el-date-picker
                    v-model="startDate"
                    type="date"
                    placeholder="选择开始日期">
                </el-date-picker>
                <span class="label">至</span>
                <el-date-picker
                    v-model="endDate"
                    type="date"
                    placeholder="选择结束日期">
                </el-date-picker>
              </div>

              <!-- 标题和关键词输入 -->
              <div class="searchls">
                <span class="label">关键词：</span>
                <el-input v-model="keyword" placeholder="请输入关键词"></el-input>
              </div>
            </div>

            <div class="searchr">
              <!-- 搜索按钮 -->
              <div class="searchBut wow fadeInDown" data-wow-delay="0.4s" @click="handleSearch">
                <span>搜索</span>
              </div>
              <!-- 导出按钮 -->
              <div class="exportBut wow fadeInDown" data-wow-delay="0.8s" @click="getDataExport">
                <span>导出</span>
              </div>
            </div>
          </div>
          <div class="pageInnerData">
            <el-table
                :data="list"
                row-key="id"
                height="100%"
            >
              <el-table-column
                  label="序号"
                  type="index"
                  width="100"
                  align="center"
              >
              </el-table-column>

              <el-table-column
                  prop="cameraIP"
                  label="摄像头IP"
                  align="center"
              >
              </el-table-column>
              <el-table-column
                  prop="operator"
                  align="center"
                  label="操作用户"
              >
              </el-table-column>
              <el-table-column
                  prop="operationTime"
                  align="center"
                  label="操作时间">
              </el-table-column>
              <el-table-column
                  prop="operationContent"
                  align="center"
                  label="操作内容">
              </el-table-column>
            </el-table>
            <div class="pagination">
              <el-pagination
                  background
                  @current-change="handlePageChange"
                  layout="prev, pager, next"
                  :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </item>
    </div>
    <videoBg></videoBg>
  </div>
</template>

<script>
import top from "../../components/top/index.vue";
import videoBg from "@/components/videoBg/index.vue";
import item from "@/components/item2/index.vue";
import WOW from "wow.js";
import {alertLogs, alertLogsExport} from "../../api/api/LargeScreenData.js";
import breadcrumb from "../../components/breadcrumb/index.vue";
import * as XLSX from 'xlsx';
import {saveAs} from 'file-saver';

export default {
  data() {
    return {
      total: 0,
      list: [],
      page: 1,
      startDate: '',
      endDate: '',
      keyword: '',
      listExport: []
    }
  },
  components: {
    top,
    videoBg,
    item,
    breadcrumb
  },
  mounted() {
    var wow = new WOW({
      boxClass: "wow",
      animateClass: "animated",
    });
    wow.init();
    this.getData()
  },
  methods: {
    // 搜索
    handleSearch() {
      this.page = 1
      this.getData()
    },
    handlePageChange(newPage) {
      // 触发接口请求
      this.page = newPage
      this.getData()
    },
    async getData() {
      try {
        const res = await alertLogs({page: this.page});
        this.list = res.data.list;
        this.total = res.data.total;
      } catch (err) {
        console.error(err);
      }
    },
    // 获取导出数据
    async getDataExport() {
      try {
        const res = await alertLogsExport();
        this.listExport = res.data.map(item => ({
          摄像头IP: item.cameraIP,  // 假设原数据字段为 cameraIP
          操作用户: item.operator,      // 假设原数据字段为 user
          操作时间: item.operationTime,      // 假设原数据字段为 time
          操作内容: item.operationContent     // 假设原数据字段为 action
        }));
        // 导出数据
        const ws = XLSX.utils.json_to_sheet(this.listExport);
        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
        const xlsxData = XLSX.write(wb, {bookType: 'xlsx', type: 'array'});
        const blob = new Blob([xlsxData], {type: 'application/octet-stream'});
        saveAs(blob, 'data.xlsx');
      } catch (err) {
        console.error(err);
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  position: fixed;
  height: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: nowrap;
  flex-direction: column;
}

.homeMain {
  position: relative;
  width: calc(100% - 60px);
  height: calc(100% - 160px);
  margin-top: 140px;
  display: flex;
  margin-left: 30px;
  z-index: 1;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;

  .pageInner {
    width: 100%;
    position: relative;
    height: 100%;

    .breadcrumb {
      width: calc(100% - 40px);
      margin-left: 20px;
      position: relative;
      height: 40px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: nowrap;
      flex-direction: row;
      align-content: flex-start;
    }

    .pageInnerData {
      position: relative;
      width: calc(100% - 40px);
      margin: 10px auto;
      height: calc(100% - 170px);
    }
  }
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
  height: 50px;
}

.detail {
  width: 100%;
  height: 46px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;

  img {
    width: 60px;
    height: 60px;
  }
}

.searchTop {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
  margin-left: 20px;
  margin-right: 20px;
  height: 60px;

  .searchl {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: nowrap;
    flex-direction: row;
    align-content: flex-start;
    color: #82b3db;
    font-size: 14px;

    .searchls {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: nowrap;
      flex-direction: row;
      align-content: flex-start;
    }

    .label {
      margin-right: 10px;
      margin-left: 10px;
      font-size: 14px;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .label2 {
      margin-right: 10px;
    }
  }

  .searchr {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex-wrap: nowrap;
    flex-direction: row;
    align-content: flex-start;
  }
}

.searchBut {
  background: url("./assets/butbg.png");
  width: 105px;
  height: 42px;
  cursor: pointer;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
  margin-bottom: -5px;

  span {
    padding-bottom: 5px;
    font-size: 14px;
  }
}

.exportBut {
  background: url("./assets/butbg2.png");
  width: 105px;
  height: 42px;
  cursor: pointer;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
  margin-bottom: -5px;

  span {
    padding-bottom: 5px;
    font-size: 14px;
  }
}
</style>
